var sx = 0;
var sy = 0;
var bx = 0;
var by = 0;

var asteroidOn = [];
asteroidOn[0] = true;
asteroidOn[1] = true;
var asteroid = [];
var viewport = $('.viewport>div:nth-child(4)');
asteroid[0] = $(viewport).find('>div:nth-child(2)');
var bullet = $(viewport).find('>div:nth-child(3)');
var start = $(viewport).find('>div:nth-child(4)');
var ship = [];
ship[0] = $(viewport).find('>div:nth-child(5)');
ship[1] = $(viewport).find('>div:nth-child(6)');
ship[2] = $(viewport).find('>div:nth-child(7)');
ship[3] = $(viewport).find('>div:nth-child(8)');
ship[4] = $(viewport).find('>div:nth-child(9)');
ship[5] = $(viewport).find('>div:nth-child(10)');
ship[6] = $(viewport).find('>div:nth-child(11)');
ship[7] = $(viewport).find('>div:nth-child(12)');
ship[8] = $(viewport).find('>div:nth-child(13)');
ship[9] = $(viewport).find('>div:nth-child(14)');
ship[10] = $(viewport).find('>div:nth-child(15)');
ship[11] = $(viewport).find('>div:nth-child(16)');
ship[12] = $(viewport).find('>div:nth-child(17)');
ship[13] = $(viewport).find('>div:nth-child(18)');
ship[14] = $(viewport).find('>div:nth-child(19)');
ship[15] = $(viewport).find('>div:nth-child(20)');
ship[16] = $(viewport).find('>div:nth-child(21)');
ship[17] = $(viewport).find('>div:nth-child(22)');
ship[18] = $(viewport).find('>div:nth-child(23)');
ship[19] = $(viewport).find('>div:nth-child(24)');
ship[20] = $(viewport).find('>div:nth-child(25)');
ship[21] = $(viewport).find('>div:nth-child(26)');
ship[22] = $(viewport).find('>div:nth-child(27)');
ship[23] = $(viewport).find('>div:nth-child(28)');

var fragment = [];
var fragmentOn = [];
var fragmentVx = [];
var fragmentVy = [];
fragment[0] = $(viewport).find('>div:nth-child(30)');
fragment[1] = $(viewport).find('>div:nth-child(31)');
fragment[2] = $(viewport).find('>div:nth-child(32)');
asteroid[1] = $(viewport).find('>div:nth-child(33)');
fragment[3] = $(viewport).find('>div:nth-child(34)');
fragment[4] = $(viewport).find('>div:nth-child(35)');
fragment[5] = $(viewport).find('>div:nth-child(36)');
fragmentOn[0] = false;
fragmentOn[1] = false;
fragmentOn[2] = false;
fragmentOn[3] = false;
fragmentOn[4] = false;
fragmentOn[5] = false;

for (var s = 1; s < 24; s++) {
    ship[s].hide();
}

for (var f = 0; f < 6; f++) {
    fragment[f].hide();
}

$(viewport).find('>div:nth-child(29)').hide();

$(start).hide();
$(bullet).hide();


var volX = 0.0;
var volY = 0.0;
var rot = 0;
var bvolX = 0.0;
var bvolY = 0.0;
var bage = 0;


$('body').append('<div style="position: absolute; top: 37px; left: 360px; color: #ffffff;">Ship Control Input: <input style="background-color: #ffa8a8;" id="ship-controls"/></div>');

$('#ship-controls').live('keydown', function(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    var sx = ship[rot].position().left;
    var sy = ship[rot].position().top;
    if (code == 39) {
        ship[rot].hide();
        rot ++;
        if (rot == 24) {rot = 0}
        ship[rot].css('top', sy).css('left', sx);
        ship[rot].show();
    } else if (code == 37) {
        ship[rot].hide();
        rot --;
        if (rot == -1) {rot = 23}
        ship[rot].css('top', sy).css('left', sx);
        ship[rot].show();
    } else if (code == 32) {
        if (bage == 0) {
            bvolX = volX;
            bvolY = volY;
            var m = 8;
            if (rot == 0) {
                bvolY = bvolY - 1 * m;
            } else if (rot == 1) {
                bvolX = bvolX + 0.15 * m;
                bvolY = bvolY - 0.9 * m;
            } else if (rot == 2) {
                bvolX = bvolX + 0.4 * m;
                bvolY = bvolY - 0.75 * m;
            } else if (rot == 3) {
                bvolX = bvolX + 0.65 * m;
                bvolY = bvolY - 0.65 * m;
            } else if (rot == 4) {
                bvolX = bvolX + 0.75 * m;
                bvolY = bvolY - 0.4 * m;
            } else if (rot == 5) {
                bvolX = bvolX + 0.9 * m;
                bvolY = bvolY - 0.15 * m;
            } else if (rot == 6) {
                bvolX = bvolX + 1 * m;
            } else if (rot == 7) {
                bvolX = bvolX + 0.9 * m;
                bvolY = bvolY + 0.15 * m;
            } else if (rot == 8) {
                bvolX = bvolX + 0.75 * m;
                bvolY = bvolY + 0.4 * m;
            } else if (rot == 9) {
                bvolX = bvolX + 0.65 * m;
                bvolY = bvolY + 0.65 * m;
            } else if (rot == 10) {
                bvolX = bvolX + 0.4 * m;
                bvolY = bvolY + 0.75 * m;
            } else if (rot == 11) {
                bvolX = bvolX + 0.15 * m;
                bvolY = bvolY + 0.9 * m;
            } else if (rot == 12) {
                bvolY = bvolY + 1 * m;
            } else if (rot == 13) {
                bvolX = bvolX - 0.15 * m;
                bvolY = bvolY + 0.9 * m;
            } else if (rot == 14) {
                bvolX = bvolX - 0.4 * m;
                bvolY = bvolY + 0.75 * m;
            } else if (rot == 15) {
                bvolX = bvolX - 0.65 * m;
                bvolY = bvolY + 0.65 * m;
            } else if (rot == 16) {
                bvolX = bvolX - 0.75 * m;
                bvolY = bvolY + 0.4 * m;
            } else if (rot == 17) {
                bvolX = bvolX - 0.9 * m;
                bvolY = bvolY + 0.15 * m;
            } else if (rot == 18) {
                bvolX = bvolX - 1 * m;
            } else if (rot == 19) {
                bvolX = bvolX - 0.9 * m;
                bvolY = bvolY - 0.15 * m;
            } else if (rot == 20) {
                bvolX = bvolX - 0.75 * m;
                bvolY = bvolY - 0.4 * m;
            } else if (rot == 21) {
                bvolX = bvolX - 0.65 * m;
                bvolY = bvolY - 0.65 * m;
            } else if (rot == 22) {
                bvolX = bvolX - 0.4 * m;
                bvolY = bvolY - 0.75 * m;
            } else if (rot == 23) {
                bvolX = bvolX - 0.15 * m;
                bvolY = bvolY - 0.9 * m;
            }
            bage = 1;
            bullet.css('top', sy + 22).css('left', sx + 22).show();
        }
    } else if (code == 38) {
        if (rot == 0) {
            volY = volY - 1;
        } else if (rot == 1) {
            volX = volX + 0.15;
            volY = volY - 0.9;
        } else if (rot == 2) {
            volX = volX + 0.4;
            volY = volY - 0.75;
        } else if (rot == 3) {
            volX = volX + 0.65;
            volY = volY - 0.65;
        } else if (rot == 4) {
            volX = volX + 0.75;
            volY = volY - 0.4;
        } else if (rot == 5) {
            volX = volX + 0.9;
            volY = volY - 0.15;
        } else if (rot == 6) {
            volX = volX + 1;
        } else if (rot == 7) {
            volX = volX + 0.9;
            volY = volY + 0.15;
        } else if (rot == 8) {
            volX = volX + 0.75;
            volY = volY + 0.4;
        } else if (rot == 9) {
            volX = volX + 0.65;
            volY = volY + 0.65;
        } else if (rot == 10) {
            volX = volX + 0.4;
            volY = volY + 0.75;
        } else if (rot == 11) {
            volX = volX + 0.15;
            volY = volY + 0.9;
        } else if (rot == 12) {
            volY = volY + 1;
        } else if (rot == 13) {
            volX = volX - 0.15;
            volY = volY + 0.9;
        } else if (rot == 14) {
            volX = volX - 0.4;
            volY = volY + 0.75;
        } else if (rot == 15) {
            volX = volX - 0.65;
            volY = volY + 0.65;
        } else if (rot == 16) {
            volX = volX - 0.75;
            volY = volY + 0.4;
        } else if (rot == 17) {
            volX = volX - 0.9;
            volY = volY + 0.15;
        } else if (rot == 18) {
            volX = volX - 1;
        } else if (rot == 19) {
            volX = volX - 0.9;
            volY = volY - 0.15;
        } else if (rot == 20) {
            volX = volX - 0.75;
            volY = volY - 0.4;
        } else if (rot == 21) {
            volX = volX - 0.65;
            volY = volY - 0.65;
        } else if (rot == 22) {
            volX = volX - 0.4;
            volY = volY - 0.75;
        } else if (rot == 23) {
            volX = volX - 0.15;
            volY = volY - 0.9;
        }
    } 
    return false;
});

function handleFragments() {
    for (var f = 0; f < 6; f++) {
        if (fragmentOn[f]) {
          var fx = fragment[f].position().left;
          var fy = fragment[f].position().top;
          if (fy < -80) {
            fy = 580;
          }
          if (fy > 580) {
            fy = -80;
          }
          if (fx < -80) {
            fx = 900;
          }
          if (fx > 900) {
            fx = -80;
          }
          fragment[f].css('top', fy + fragmentVy[f]).css('left', fx + fragmentVx[f]);

          if (bage > 0 && bx > fx && bx < fx + 70 && by > fy && by < fy + 70) {
            bage = 0;
            bullet.hide();
            fragment[f].hide();
            fragmentOn[f] = false;
          }

        }
    }
}

function handleAsteroid(index, vx, vy) {

  if (asteroidOn[index]) {
      var ax = asteroid[index].position().left;
      var ay = asteroid[index].position().top;
      if (ay < -160) {
        ay = 580;
      }
      if (ay > 580) {
        ay = -160;
      }
      if (ax < -160) {
        ax = 900;
      }
      if (ax > 900) {
        ax = -160;
      }
      asteroid[index].css('top', ay + vy).css('left', ax + vx);

      //check collision
      if (bage > 0 && bx > ax && bx < ax + 120 && by > ay && by < ay + 120) {
        bage = 0;
        bullet.hide();
        asteroid[index].hide();
        asteroidOn[index] = false;
        if (index == 0) {
            fragment[0].css('top', ay).css('left', ax);
            fragment[0].show();
            fragmentVx[0] = -2;
            fragmentVy[0] = -1.8;
            fragmentOn[0] = true;
            fragment[1].css('top', ay + 10).css('left', ax + 20);
            fragment[1].show();
            fragmentVx[1] = 2.2;
            fragmentVy[1] = -1.5;
            fragmentOn[1] = true;
            fragment[2].css('top', ay + 20).css('left', ax + 10);
            fragment[2].show();
            fragmentVx[2] = 2;
            fragmentVy[2] = 2.2;
            fragmentOn[2] = true;
        } else {
            fragment[3].css('top', ay).css('left', ax);
            fragment[3].show();
            fragmentOn[3] = true;
            fragmentVx[3] = -1.8;
            fragmentVy[3] = -2.4;
            fragment[4].css('top', ay + 10).css('left', ax + 20);
            fragment[4].show();
            fragmentOn[4] = true;
            fragmentVx[4] = 2.3;
            fragmentVy[4] = -1.1;
            fragment[5].css('top', ay + 20).css('left', ax + 10);
            fragment[5].show();
            fragmentOn[5] = true;
            fragmentVx[5] = 2.1;
            fragmentVy[5] = 1.9;
        }
      }

  }
  
}


function run() {

  //move ship
  sx = ship[rot].position().left;
  sy = ship[rot].position().top;
  if (sy < -80) {
    sy = 560;
  }
  if (sy > 560) {
    sy = -80;
  }
  if (sx > 900) {
    sx = -80;
  }
  if (sx < -80) {
    sx = 900
  }
  ship[rot].css('top', sy + volY).css('left', sx + volX);
  ship[rot].show();

  //doBullet
  bx = bullet.position().left;
  by = bullet.position().top;
  if (by < -20) {
    by = 560;
  }
  if (by > 560) {
    by = -20;
  }
  if (bx > 900) {
    bx = -20;
  }
  if (bx < -20) {
    bx = 900
  }
  bullet.css('top', by + bvolY).css('left', bx + bvolX);
  if (bage > 0) {
    bage ++;
    if (bage > 100) {
      bage = 0;
      bullet.hide();
    }
  }

  handleAsteroid(0, -2, -1);
  handleAsteroid(1, 1, 2);

  handleFragments();

  setTimeout("run()",50);
}

run();



